package tv.youxing.android.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import tv.youxing.android.R;
import tv.youxing.android.custom.CustomToast;
import tv.youxing.android.entity.VideoPlayerPostModel;
import tv.youxing.android.entity.ZhuanJiFenLeiListModel;
import tv.youxing.android.utils.AppConst;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.ShareUtils;
import tv.youxing.android.utils.StaticClass;
import tv.youxing.android.utils.UtilTools;

/**
 * Created by Administrator on 2017/7/19.
 * 首页模块更多
 */

public class ModuleMoreDetailsActivity extends BaseActivity {

    @BindView(R.id.pull_refresh_grid)
    PullToRefreshGridView mPullRefreshListView;

    int album_classify_id;

    Adapter mAdapter;
    int curPageCount=0;

    //首页-分类滑动
    private ZhuanJiFenLeiListModel mAllData;
    private List<ZhuanJiFenLeiListModel.ResultBean.DataBean> mListData = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuan_ji_fen_lei);
        ButterKnife.bind(this);

        album_classify_id = Integer.parseInt(getIntent().getStringExtra("album_classify_id"));

        initData();
        loadData();
    }

    private void loadData() {
        //post请求简洁版实现
        curPageCount++;
        HttpParams params = new HttpParams();
        params.put("album_classify_id",this.album_classify_id);
        params.put("page",curPageCount);
        if (UtilTools.LoginJudge()) {
            params.put("user_id",ShareUtils.getInt(this, AppConst.USERID,0));
        }

        RxVolley.post(StaticClass.ZhuanJiFenLeiList, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                mAllData = GsonUtils.GsonToBean(t,ZhuanJiFenLeiListModel.class);
                if (mAllData.getCode() == 200) {
                    if (mAllData.getResult().getData().size() > 0) {
                        mListData.addAll(mAllData.getResult().getData());
                        mAdapter.notifyDataSetChanged();
                    } else {
                        CustomToast.INSTANCE.showToast("没数据了");
                    }


                    if (mPullRefreshListView.isRefreshing()) {
                        mPullRefreshListView.onRefreshComplete();
                    }

                } else {
                    CustomToast.INSTANCE.showToast("无数据");
                }
            }
        });
    }

    void initData() {
        mAdapter = new Adapter(this, mListData);
        mPullRefreshListView.setAdapter(mAdapter);
        mPullRefreshListView
                .setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<GridView>()
                {
                    @Override
                    public void onRefresh(
                            PullToRefreshBase<GridView> refreshView)
                    {
                        String label = DateUtils.formatDateTime(
                                getApplicationContext(),
                                System.currentTimeMillis(),
                                DateUtils.FORMAT_SHOW_TIME
                                        | DateUtils.FORMAT_SHOW_DATE
                                        | DateUtils.FORMAT_ABBREV_ALL);
                        // 显示最后更新的时间
//                        refreshView.getLoadingLayoutProxy()
//                                .setLastUpdatedLabel(label);

                        // 模拟加载任务
                        loadData();
                    }
                });
        mPullRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ZhuanJiFenLeiListModel.ResultBean.DataBean m = mListData.get(i);
                VideoPlayerPostModel model = new VideoPlayerPostModel();
                model.setMultimedia_id(m.getMultimedia_id());
                model.setTitle(m.getTitle());
                model.setIntro(m.getIntro());
                model.setCover(m.getCover());
                model.setDuration(m.getDuration());
                model.setPublish_at(m.getPublish_at());
                model.setPlay_url(m.getPlay_url());
                model.setType(m.getType());
                model.setPlay_count(m.getPlay_count());
                model.setComment_count(m.getComment_count());
                model.setLike_count(m.getLike_count());
                model.setCollection_count(m.getCollection_count());
                model.setStatus(m.getStatus());
                model.setCreated_at(m.getCreated_at());
                model.setUpdated_at(m.getUpdated_at());
                model.setIs_like(m.getIs_like());
                model.setIs_collectied(m.getIs_collectied());
                model.setVideo_share_url(m.getVideo_share_url());
                VideoPlayerPostModel.AlbumBean albumBean = new VideoPlayerPostModel.AlbumBean();
                albumBean.setAlbum_id(m.getAlbum().getAlbum_id());
                albumBean.setName(m.getAlbum().getName());
                albumBean.setCover(m.getAlbum().getCover());
                albumBean.setIs_order(m.getAlbum().getIs_order());
                model.setAlbum(albumBean);

                Intent intent = new Intent(ModuleMoreDetailsActivity.this, SubscribeDeteilsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data",model);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    class Adapter extends BaseAdapter {

        private Context context;
        private List<ZhuanJiFenLeiListModel.ResultBean.DataBean> listData = new ArrayList<>();
        private LayoutInflater layoutInflater;


        public Adapter(Context context, List<ZhuanJiFenLeiListModel.ResultBean.DataBean> listData) {
            this.context = context;
            this.listData = listData;
            layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return listData==null || listData.size()==0 ? 0 : listData.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            ViewHolder viewHolder = null;
            if (viewHolder==null) {
                viewHolder = new ViewHolder();
                convertView = layoutInflater.inflate(R.layout.layout_item_video, null);
                viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
                viewHolder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }

            ImageLoader.getInstance().displayImage(listData.get(position).getCover(),viewHolder.ivIcon);
            viewHolder.tvName.setText(listData.get(position).getTitle()+ listData.get(position).getMultimedia_id());
            viewHolder.tvName.setLines(1);

            return convertView;
        }


        class ViewHolder{
            private ImageView ivIcon;
            private TextView tvName;
        }
    }
}
